package codegen.util.database;

public class Field implements java.io.Serializable {

	private static final long serialVersionUID = -7355769098073479436L;

	private String tableName;

	private String tableDesc;

	private String fieldName;

	private String fieldDesc;

	private PrimaryKey primaryKey;

	private boolean condition;

	private boolean display;

	private DataType dataType;

	private String dataTypeName;

	private String columnDataType;

	private int width;

	private int decimalWidth;

	private Object value;

	private boolean nullable;

	private Object defaultValue;
	private boolean isPk;
	private int fieldSeq;

	public String getDataTypeName() {
		return dataTypeName;
	}

	public void setDataTypeName(String dataTypeName) {
		this.dataTypeName = dataTypeName;
	}

	public String getTableDesc() {
		return tableDesc;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public void setTableDesc(String tableDesc) {
		this.tableDesc = tableDesc;
	}

	public void setFieldDesc(String fieldDesc) {
		this.fieldDesc = fieldDesc;
	}

	public void setDataType(DataType dataType) {
		this.dataType = dataType;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public void setDecimalWidth(int decimalWidth) {
		this.decimalWidth = decimalWidth;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setCondition(boolean condition) {
		this.condition = condition;
	}

	public void setDisplay(boolean display) {
		this.display = display;
	}

	public void setPrimaryKey(PrimaryKey primaryKey) {
		this.primaryKey = primaryKey;
	}

	public String getTableName() {
		return tableName;
	}

	public String getFieldDesc() {
		return fieldDesc;
	}

	public DataType getDataType() {
		return dataType;
	}

	public String getFieldName() {
		return fieldName;
	}

	public int getDecimalWidth() {
		return decimalWidth;
	}

	public int getWidth() {
		return width;
	}

	public boolean isCondition() {
		return condition;
	}

	public boolean isDisplay() {
		return display;
	}

	public PrimaryKey getPrimaryKey() {
		return primaryKey;
	}

	public Field(String tableName, String fieldName) {
		this.tableName = tableName;
		this.fieldName = fieldName;
	}

	public Field(String tableName, String tableDesc, String fieldName,
			String fieldDesc, PrimaryKey primaryKey, boolean condition,
			boolean display, String columnDataType, DataType dataType,
			int width, int decimalWidth, Object value, int fieldSeq,
			boolean nullable, boolean isPk) {
		this.tableName = tableName;
		this.tableDesc = tableDesc;
		this.fieldName = fieldName;
		this.fieldDesc = fieldDesc;
		this.primaryKey = primaryKey;
		this.condition = condition;
		this.display = display;
		this.dataType = dataType;
		this.width = width;
		this.decimalWidth = decimalWidth;
		this.value = value;
		this.nullable = nullable;
		this.isPk = isPk;
		this.columnDataType = columnDataType;
		this.fieldSeq = fieldSeq;
	}

	public Field(Field field) {
		this.tableName = field.tableName;
		this.tableDesc = field.tableDesc;
		this.fieldName = field.fieldName;
		this.fieldDesc = field.fieldDesc;
		this.primaryKey = field.primaryKey;
		this.condition = field.condition;
		this.display = field.display;
		this.dataType = field.dataType;
		this.width = field.width;
		this.decimalWidth = field.decimalWidth;
		this.value = field.value;
	}

	public Field(String fieldName, Object value) {
		this.fieldName = fieldName;
		this.value = value;
	}

	public String toSQL() {

		return this.tableName
				+ "."
				+ (this.fieldName.length() > Field.MAX_FIELD_LENGTH ? this.fieldName
						.substring(this.fieldName.length()
								- Field.MAX_FIELD_LENGTH) : this.fieldName);
	}

	public String getClassName() {
		String s = this.fieldName.substring(0, 1).toUpperCase()
				+ this.fieldName.substring(1).toLowerCase();
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '_') {
				s = s.substring(0, i) + (s.charAt(i + 1) + "").toUpperCase()
						+ s.substring(i + 2);
			}
		}
		s = s.replaceAll("Xml", "XML");
		s = s.replaceAll("Sdi", "SDI");
		return s;

	}

	public String getObjectName() {
		String s = this.fieldName.toLowerCase();
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '_') {
				s = s.substring(0, i) + (s.charAt(i + 1) + "").toUpperCase()
						+ s.substring(i + 2);
			}
		}
		s = s.replaceAll("Xml", "XML");
		s = s.replaceAll("Sdi", "SDI");
		return s;
	}

	public boolean isPrimaryKey() {
		return this.primaryKey != null;
	}

	public static int MAX_FIELD_LENGTH = 50;

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public boolean isNullable() {
		return nullable;
	}

	public boolean isPk() {
		return isPk;
	}

	public void setPk(boolean isPk) {
		this.isPk = isPk;
	}

	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}

	public Object getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(Object defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getColumnDataType() {
		return columnDataType;
	}

	public void setColumnDataType(String columnDataType) {
		this.columnDataType = columnDataType;
	}

	public int getFieldSeq() {
		return fieldSeq;
	}

	public void setFieldSeq(int fieldSeq) {
		this.fieldSeq = fieldSeq;
	}

}
